<?php
require_once "settings.inc";
require_once "rtvFunctions.inc";
require_once "parsers.inc";
require_once "renderer.inc";

# renew the list using existing cookie
$functions = new RtvFunctions();
$rawList = $functions->getChannelsList();	

# parse raw list into prepared class hierarchy
$parser = new ChannelsParser();
$parser->parse($rawList);
$categories = $parser->categories;

# decide what to show
if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $itemsToDisplay = array();
    foreach ($categories as $category) {
        if ($id == 0 || $id == $category->id) {
    	    $itemsToDisplay[] = $category;
        }
    }
    print drawChannelPage($itemsToDisplay);
} else {
    $itemsToDisplay = array();
    foreach ($categories as $category) {
        if (0 == $category->id || count($category->channels) > 0) {
    	    $itemsToDisplay[] = $category;
        }
    }
    print drawCategoryPage($itemsToDisplay);
}
?>
